<template>
  <view>
    <uv-toast ref="deleteToastRef" position="top"></uv-toast>
    <!-- 确认删除弹出框 -->
    <uv-popup ref="popupDelete" mode="center" round="20">
      <view style="width: 290rpx; height: 70rpx; text-align: center">
        <span
          style="
            font-size: 35rpx;
            color: black;
            display: block;
            font-weight: bolder;
            padding: 10rpx;
            transform: translateY(15rpx);
          "
          >确定删除它吗?</span
        >
      </view>
      <view class="ok" @click="debounce_Delete()"> 确认 </view>
    </uv-popup>
    <uni-card :cover="cover" @click="onClick" v-for="item in postList" :key="item.id">
      <template v-slot:cover>
        <image style="width: 100%" :src="item.url" mode="aspectFill"></image>
      </template>
      <text class="uni-body">{{ item.title }}</text>
      <template v-slot:actions>
        <view class="card-actions">
          <view class="card-actions-item" @click="actionsClick('管理', item.id)">
            <uni-icons type="chatbubble" size="18" color="#999"></uni-icons>
            <text class="card-actions-item-text">管理评论</text>
          </view>
          <view class="card-actions-item" @click="actionsClick('删除', item.id)">
            <uni-icons type="trash" size="18" color="#999"></uni-icons>
            <text class="card-actions-item-text">删除帖子</text>
          </view>
        </view>
      </template>
    </uni-card>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { getUserAllPostAPI, deletePostAPI } from '@/services/forum'
import { onReachBottom } from '@dcloudio/uni-app'
import { onLoad } from '@dcloudio/uni-app'
import { debounce } from '@/utils/debounce'
onLoad(async () => {
  const res = await getUserAllPostAPI()
  postList.value = res.data
})
const deleteToastRef = ref()
const postList = ref()
const popupDelete = ref()
const actionsClick = async (type, id) => {
  if (type === '管理') {
    uni.navigateTo({ url: `/pages/forum/info?id=${id}` })
  } else {
    toDeleteId.value = id
    popupDelete.value.open()
  }
}
const toDeleteId = ref()
//确认删除
const Delete = async () => {
  await deletePostAPI(toDeleteId.value)
  popupDelete.value.close()
  const res = await getUserAllPostAPI()
  postList.value = res.data
  uni.hideLoading()
  deleteToastRef.value.show({
    type: 'success',
    message: '删除成功',
  })
}
const debounce_Delete = debounce(Delete, 500)
</script>

<style lang="scss">
.container {
  overflow: hidden;
}

.card-actions {
  display: flex;
  flex-direction: row;
  justify-content: space-around;
  align-items: center;
  height: 45px;
  border-top: 1px #eee solid;
}
.card-actions-item {
  display: flex;
  flex-direction: row;
  align-items: center;
}
.card-actions-item-text {
  font-size: 12px;
  color: #666;
  margin-left: 5px;
}
.cover-image {
  flex: 1;
  height: 150px;
}
.ok {
  width: 50%;
  padding: 8px 0;
  text-align: center;
  background-color: #1a966c;
  border-radius: 20px;
  color: #fff;
  margin: 0 auto;
  margin-top: 20rpx;
  margin-bottom: 20rpx;
}
</style>
